<template>
  <div class="wrapLimit clx main">
    <div class="inventory_control">
      <div class="form-baseinfo">
        <el-form ref="inputModel" :model="inputModel" :inline="true" label-width="80px" class="form-baseinfo">
          <div class="inventoryInfo">
            <p class="title">库存调整</p>
            <div class="info">
              <el-form-item label="物料类型">
                <el-select v-model="inputModel.centreType" filterable @change="centreTypeChange">
                  <el-option v-for="item in centreType" :key="item.label" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item label="物料名称">
                <el-select v-model="inputModel.pNo" filterable>
                  <el-option v-for="item in types" :key="item.id" :label="item.pTitle" :value="item.pNo" />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button size="medium" type="text" style="color: #0076ff" @click="insertData">+ 添加</el-button>
              </el-form-item>
            </div>
          </div>
          <LPTablePage
            :show-index="true"
            :show-indexfixed="false"
            index-label="序号"
            :table-data="tableData"
            :table-column="tableColumn"
            :show-page="false"
            align="center"
          >
            <template slot="pCount" slot-scope="{ row }">
              <el-input-number v-model="row.pCount" size="mini" :precision="0" />
            </template>
          </LPTablePage>
          <el-form-item>
            <span class="text">注：库存变更，为正，则为入库库存，为负，则为出库库存。</span>
          </el-form-item>
        </el-form>
        <div class="form-baseinfo">
          <el-button class="button" size="medium" @click="close">关闭</el-button>
          <el-button class="button" size="medium" type="primary" @click="Save">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCategoryInfo } from '@/api/LaborProtection/categoryInfo'
import { getProductList } from '@/api/LaborProtection/product'
import { getAddProductInventoryList, UpdateProductInventory } from '@/api/LaborProtection/productInventory'
import { closeActiveTag } from '@/utils/lpUtils.js'
import LPTablePage from '@/components/LPTablePage/index.vue'

export default {
  name: 'InventoryControl',
  components: { LPTablePage },
  data() {
    return {
      inputModel: {
        centreType: '',
        pNo: ''
      },
      // 列表数据
      tableData: [],
      // 表格字段
      tableColumn: [
        { label: '物料类型', prop: 'pClassName' },
        { label: '物料编码', prop: 'pNo' },
        { label: '物料名称', prop: 'pTitle', width: 330 },
        { label: '计量单位', prop: 'pUnit' },
        { label: '劳保库存', prop: 'plbQuantity' },
        { label: '劳保库存变更', prop: 'pCount', render: true, width: 300 }
      ],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      types: [],
      bigType: [],
      centreType: [],
      smallType: []
    }
  },
  mounted() {
    this.getCategoryInfo(2)
  },
  methods: {
    setDropDownStructure(maps) {
      return maps.map((item) => {
        return {
          label: item.cName,
          value: item.categoryNo
        }
      })
    },
    /**
     *  1：大类 2：中类 3：小类
     * @param {*} type
     */
    getCategoryInfo(type) {
      const data = {
        CCLass: type,
        SkipCount: 0
      }
      getCategoryInfo(data).then((res) => {
        if (!res.error) {
          if (type == 1) {
            this.bigType = res.items
          } else if (type == 2) {
            this.centreType = this.setDropDownStructure(res.items)
          } else {
            this.smallType = this.setDropDownStructure(res.items)
          }
        }
      })
    },
    getProductList(QCategoryNo) {
      const data = {
        QCategoryNo: QCategoryNo,
        SkipCount: 0
      }
      getProductList(data).then((res) => {
        this.types = res.items
      })
    },
    Save() {
      const data = {
        childList: []
      }
      this.tableData.forEach((item) => {
        data.childList.push({
          pNo: item.pNo,
          pTitle: item.pTitle,
          pCount: item.pCount
        })
      })
      UpdateProductInventory(data).then((res) => {
        if (!res.error) {
          this.$message.success('保存成功')
          this.close()
        }
      })
    },
    centreTypeChange(v) {
      this.getProductList(v)
      this.inputModel.pTitle = ''
    },
    insertData() {
      let falg = false
      this.tableData.forEach((item) => {
        if (item.pNo == this.inputModel.pNo) {
          falg = true
        }
      })
      if (falg) {
        return this.$message.error('不可重复添加同一物料')
      }
      const params = {
        pNo: this.inputModel.pNo,
        SkipCount: 0
      }
      getAddProductInventoryList(params).then((res) => {
        if (!res.error) {
          res.items.forEach((item) => {
            item.pCount = 0
            this.tableData.push(item)
          })
        }
      })
    },
    close() {
      closeActiveTag(this, '/laborProtection/inventoryControl')
    }
  }
}
</script>

<style lang="scss">
.el-form-item__label {
  font-weight: normal;
}

.el-dialog__body {
  padding-top: 0px;
}

.el-divider {
  margin-top: 5px;
}
</style>

<style lang="scss" scoped>
.inventory_control {
  margin: 0 45px;

  .form-baseinfo {
    ::v-deep .search-bar input.el-input__inner {
      width: 2rem;
    }
    .text {
      color: red;
    }
    .title {
      font-size: 16px;
      font-weight: 600;
    }

    ::v-deep .el-form-item {
    }
  }
}

::v-deep .el-form-item__label {
  text-align: left;
  font-weight: normal;
  color: #666666;
  line-height: 30px;
}
.inventoryInfo {
  .info {
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-start;
    align-items: end;
    // margin: 0 45px;
    border-bottom: 1px solid #ededed;

    .el-form-item {
      display: flex;
      flex-direction: column;
      // width: calc(33%);
      height: auto;
      margin-bottom: 80px;
      &:first-child {
        margin-right: 200px;
      }
    }
    ::v-deep .el-input__inner {
      width: 400px;
      height: 36px;
      background: #ffffff;
      border-radius: 4px;
      border: 1px solid #d8d8d8;
    }
  }
}

.title {
  margin-left: 10px;
  padding: 20px 0 20px 0;
  font-size: 16px;
  font-weight: 600;
  &::before {
    content: '';
    display: inline-block;
    width: 3px;
    height: 16px;
    position: absolute;
    left: 45px;
    background: #0076ff;
  }
}

.button {
  width: 100px;
  position: relative;
  left: 45%;
  top: 30px;
  margin: 0 40px;
  transform: translate(-50%);

  &:nth-of-type(1) {
    color: #333333;
    background-color: #fff;
  }
}
</style>
